package com.supermap.dataservice.cloud.science.assets.comm;

import cn.hutool.core.date.DateUtil;
import com.supermap.dubbo.api.cloud.assets.comm.Comm;
import com.supermap.dubbo.api.cloud.assets.comm.Commcom;
import org.apache.ibatis.annotations.*;
import org.apache.logging.log4j.util.Strings;

import java.util.List;
import java.util.Map;

/**
 * @author aqua
 * @description
 * @date 2019年10月23 17:10
 * @email 1301584033@qq.com
 */
@Mapper
public interface CommMapper {
    @Select("select pid,num from t_scientific_commodity_use_details  t1 \n" +
            "inner join t_scientific_commodity_use t2\n" +
            "on t1.useId=t2.id\n" +
            "where t1.useId=#{id} and t2.state=0   limit 1")
    Map<String,Object>  getMap(@Param("id")Integer id);


    @Select("select stype from t_scientific_product where  id=#{pid} and del_flg=0")
    Integer getStype(@Param("pid")Integer pid);

    @InsertProvider(type = CommMapperSQL.class,method = "putListConmmSQL")
    void putListConmm(@Param("list") List<Integer> list,@Param("useId") Integer usrId);
    @UpdateProvider(type = CommMapperSQL.class,method = "updateListConmmSQL")
    void updateListConmm(@Param("list") List<Integer> list,@Param("state") Integer state);

    @Select("select t1.id from t_scientific_commodity t1 where t1.stat=1 and t1.pid=#{pid} limit #{num}")
    List<Integer>  getKuCunId(@Param("pid")Integer pid,@Param("num")Integer num);
    @Select("select t1.cid from t_scientific_comm_use t1\n" +
            "inner join t_scientific_commodity_use t2\n" +
            "on t1.cuid=t2.id where t2.id=#{useId}")
    List<Integer>  getKuCunIdByUsrId(@Param("useId")Integer useId);
    @Insert("insert into t_scientific_commodity_use(userId,content,gdate) values(#{comm.userId},#{comm.content},#{comm.gdate})")
    void putComm(@Param("comm")Comm comm);
    @Insert("insert into t_scientific_commodity_use_details(useId,pid,price,num) values(#{commcom.useId},#{commcom.pid},#{commcom.price},#{commcom.num})")
    void putCommUse(@Param("commcom")Commcom commcom);
    @Select("select last_insert_id()")
    Integer getId();
    @Select("select * from t_scientific_commodity_use_details t1\n" +
            "inner join t_scientific_product t2 on t1.pid=t2.id and del_flg=0 \n" +
            "where t1.useId=#{id}")
    List<Map<String,Object>> getComList(@Param("id")Integer id);
    @SelectProvider(type = CommMapperSQL.class,method = "getCommodityUsePageListSQL")
    List<Map<String,Object>> getCommodityUsePageList( @Param("name")String  name, @Param("state")Integer state, @Param("page")Integer page, @Param("size")Integer size);
    @SelectProvider(type = CommMapperSQL.class,method = "getCommodityCountUsePageListSQL")
    Integer getCommodityCountUsePageList(@Param("name")String  name, @Param("state")Integer state);

    @SelectProvider(type = CommMapperSQL.class,method = "getCommodityPageListSQL")
    List<Map<String,Object>> getCommodityPageList(@Param("name") String name, @Param("stype")Integer stype, @Param("parenId")Integer parenId, @Param("page")Integer page, @Param("size")Integer size);

    @Insert("insert into t_scientific_product(stype,name,parenId,price,unit,conctent) values(#{stype},#{name},#{parenId},#{price},#{unit},#{conctent})")
    Integer putCommodity(@Param("stype")Integer stype, @Param("name")String name, @Param("parenId")Integer parenId,@Param("price")Double price, @Param("unit")String unit, @Param("conctent")String conctent);

    @Update("update t_scientific_product set del_flg=1 where ID=#{id}")
    Integer delCommodity(@Param("id") Integer id);

    @Select("select \n" +
            " t2.name,t2.id,count(t2.id) as countNum,\n" +
            " (select count(id) from t_scientific_commodity where stat=1 and pid=t1.pid) as wNum,\n" +
            " (select count(id) from t_scientific_commodity where stat=0 and pid=t1.pid) as sNum, \n" +
            "  (select count(id) from t_scientific_commodity where stat=2 and pid=t1.pid) as yNum\n" +
            "from t_scientific_commodity t1\n" +
            "inner join t_scientific_product t2\n" +
            "on t1.pid=t2.id and t2.del_flg=0 \n" +
            "where if(isnull(#{pId}),1=1,t2.id=#{pId})    \n" +
            "group by t2.id  limit #{limit}")
    List<Map<String,Object>> getCommodityCountList(@Param("pId") Integer pId,@Param("limit") Integer limit);

    @Select("select \n" +
            "t3.name as  spName,\n" +
            "t2.num as spNum,\n" +
            "if(t3.stype=1,'一次性用品','多次性用品') as typeName,\n" +
            "(select t.realname from  cqpamiss.t_s_base_user t  where t.id=t1.auserId) as jbName,\n" +
            "(select t.realname from  cqpamiss.t_s_base_user t  where t.id=t1.userId) as sqName,\n" +
            "if(t1.state=2,'使用中',if(t1.state=4,'结束','')) as state,\n" +
            "t1.bdate,\n" +
            "t1.gdate,\n" +
            "t1.sdate\n" +
            "from t_scientific_commodity_use t1\n" +
            "inner join t_scientific_commodity_use_details t2\n" +
            "on t1.id=t2.useId\n" +
            "inner join t_scientific_product t3\n" +
            "on t2.pid=t3.id and t3.del_flg=0\n" +
            "left join  cqpamiss.t_s_base_user t4\n" +
            "on t1.userId=t4.ID\n" +
            "where 1=1 and  t1.state=#{state}  and t3.id=#{id}")
    List<Map<String,Object>> getCommodityCountListConmm(@Param("id") Integer id,@Param("state") Integer state);
    @UpdateProvider(type = CommMapperSQL.class,method = "manageAuditSQL")
    Integer manageAudit(@Param("id")Integer id,@Param("scontent")String scontent,@Param("suserId")String suserId, @Param("state")Integer state);
    class  CommMapperSQL{
        public String updateListConmmSQL(Map map){
            StringBuilder stringBuilder=new StringBuilder();
            List<Integer> integerList=(List<Integer>)map.get("list");
            Integer state=(Integer)map.get("state");
            stringBuilder.append("update t_scientific_commodity set stat="+state+" where id in ( ");
            integerList.stream().forEach(num->{
                stringBuilder.append(""+num+",");
            });
            return  stringBuilder.substring(0,stringBuilder.length()-1)+")";
        }
        public String putListConmmSQL(Map map){
            StringBuilder stringBuilder=new StringBuilder();
            List<Integer> integerList=(List<Integer>)map.get("list");
            Integer useId=(Integer)map.get("useId");
            stringBuilder.append("insert into t_scientific_comm_use(cid,cuid) values ");
            integerList.stream().forEach(num->{
                stringBuilder.append("("+num+","+useId+"),");
            });
            return  stringBuilder.substring(0,stringBuilder.length()-1);
        }
        public String manageAuditSQL(Map map){
            StringBuilder stringBuilder=new StringBuilder();
            Integer id=(Integer)map.get("id");
            String scontent=(String)map.get("scontent");
            String suserId=(String)map.get("suserId");
            Integer state=(Integer)map.get("state");
            if(state==1){
                stringBuilder.append("update t_scientific_commodity_use set auserId='"+suserId+"',scontent='"+scontent+"',state="+state+" where id="+id+"");
            }
            if(state==2){
                stringBuilder.append("update t_scientific_commodity_use set auserId='"+suserId+"',scontent='"+scontent+"',bdate='"+ DateUtil.now() +"',state="+state+" where id="+id+"");
            }
            if(state==3){
                stringBuilder.append("update t_scientific_commodity_use set scontent='"+scontent+"',sdate='"+DateUtil.now()+"',state="+state+" where id="+id+"");
            }
            if(state==4){
                stringBuilder.append("update t_scientific_commodity_use set scontent='"+scontent+"',sdate='"+DateUtil.now()+"',state="+state+" where id="+id+"");
            }
            return  stringBuilder.toString();
        }


        public String getCommodityUsePageListSQL(Map map){
            StringBuilder stringBuilder=new StringBuilder();
            String name =(String)map.get("name");
            Integer state =(Integer)map.get("state");
            Integer page =(Integer)map.get("page");
            Integer size =(Integer)map.get("size");
            stringBuilder.append("select t1.*,\n" +
                    "                     (select realname from cqpamiss.t_s_base_user where id=t1.auserId) as aname,\n" +
                    "                    (select realname from cqpamiss.t_s_base_user where id=t1.userId) as sname\n" +
                    "                    from cagmpisards.t_scientific_commodity_use t1 \n" +
                    "                    inner join cqpamiss.t_s_base_user t2\t\n" +
                    "                    on t1.userId=t2.ID\n" +
                    "                    where 1=1 \n");
            if(Strings.isNotBlank(name)){
                stringBuilder.append(" and t2.realname like concat('%','"+name+"','%') ");
            }
            if(Strings.isNotBlank(name)){
                stringBuilder.append(" and t1.state="+state+" ");
            }
            stringBuilder.append(" limit "+(page-1)*size+","+size+" ");
            return  stringBuilder.toString();
        }

        public String getCommodityCountUsePageListSQL(Map map){
            StringBuilder stringBuilder=new StringBuilder();
            String name =(String)map.get("name");
            Integer state =(Integer)map.get("state");

            stringBuilder.append("select count(1) from cagmpisards.t_scientific_commodity_use t1 \n" +
                    "                    inner join cqpamiss.t_s_base_user t2\t\n" +
                    "                    on t1.userId=t2.ID\n" +
                    "                    where 1=1 \n");
            if(Strings.isNotBlank(name)){
                stringBuilder.append(" and t2.realname like concat('%','"+name+"','%') ");
            }
            if(Strings.isNotBlank(name)){
                stringBuilder.append(" and t1.state="+state+" ");
            }
            return  stringBuilder.toString();
        }

        public String getCommodityPageListSQL(Map map){
            StringBuilder stringBuilder=new StringBuilder();
            String name =(String)map.get("name");
            Integer stype =(Integer)map.get("stype");
            Integer parenId =(Integer)map.get("parenId");
            Integer page =(Integer)map.get("page");
            Integer size =(Integer)map.get("size");
            stringBuilder.append(" select * from t_scientific_product t1 where del_flg=0 ");

            if(Strings.isNotBlank(name)){
                stringBuilder.append(" and t1.name like concat('%','"+name+"','%') ");
            }
            if(stype!=null){
                stringBuilder.append(" and t1.stype="+stype+" ");
            }
            if(parenId!=null){
                stringBuilder.append(" and t1.parenId="+parenId+" ");
            }

            stringBuilder.append(" limit "+page+","+size+" ");
            return  stringBuilder.toString();
        }
    }

}
